package com.meiyu.live.ui.main.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.meiyu.live.BaseActivity;
import com.meiyu.live.R;
import com.meiyu.live.popj.LiveRoom;
import com.meiyu.live.ui.main.NowLiveActivity;
import com.meiyu.live.util.DisplayUtils;

import java.util.ArrayList;
import java.util.List;

public class NowHotLiveView extends FrameLayout implements View.OnClickListener {

    private static final int MAX_ITEMS = 6;

    private TextView tv_title;
    private View layout_more;
    private LinearLayout layout_hlist;

    private ImageLoader imageLoader;
    private DisplayImageOptions options;

    private List<LiveRoom> dataList = new ArrayList<>();

    public NowHotLiveView(Context context) {
        super(context);
        init(context);
    }

    public NowHotLiveView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public NowHotLiveView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    private void init(Context context) {
        imageLoader = ImageLoader.getInstance();
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.image_loading)          // 设置图片下载期间显示的图片
                .showImageForEmptyUri(R.drawable.image_loading)  // 设置图片Uri为空或是错误的时候显示的图片
                .showImageOnFail(R.drawable.image_loading)       // 设置图片加载或解码过程中发生错误显示的图片
                .cacheInMemory(true)                        // 设置下载的图片是否缓存在内存中
                .cacheOnDisk(true)                          // 设置下载的图片是否缓存在SD卡中
                .displayer(new FadeInBitmapDisplayer(200))// 图片加载好后渐入的动画时间
                .build();
        LayoutInflater inflater = LayoutInflater.from(context);
        inflater.inflate(R.layout.adapter_nowhot_item, this);
        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_title.setText("正在播出");
        layout_more = findViewById(R.id.layout_more);
        layout_more.setOnClickListener(this);
        layout_hlist = (LinearLayout) findViewById(R.id.layout_hlist);

        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        int itemWidth = (dm.widthPixels - DisplayUtils.dpToPixel(context, 8 * 2)) / 2;
        for (int i = 0; i < MAX_ITEMS; i++) {
            NowHotLiveDetailItemView itemView = new NowHotLiveDetailItemView(context);
            itemView.setImageLoader(imageLoader, options);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(itemWidth, LinearLayout.LayoutParams.WRAP_CONTENT);
            itemView.setVisibility(GONE);
            layout_hlist.addView(itemView, params);
        }
    }

    public void setData(List<LiveRoom> list) {
        if (list == null) {
            return;
        }
        dataList.clear();
        dataList.addAll(list);
        for (int i = 0; i < MAX_ITEMS || i < dataList.size(); i++) {
            if (i < MAX_ITEMS && i < dataList.size()) {
                NowHotLiveDetailItemView itemView = (NowHotLiveDetailItemView) layout_hlist.getChildAt(i);
                itemView.setVisibility(View.VISIBLE);
                itemView.setData(dataList.get(i));
            } else if (i < MAX_ITEMS) {
                layout_hlist.getChildAt(i).setVisibility(View.GONE);
            }
        }
    }

    @Override
    public void onClick(View view) {
        if (view == layout_more) {
            ((BaseActivity) getContext()).startActivity(NowLiveActivity.class);
        }
    }
}
